define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'usersauth/index',
                    add_url: 'usersauth/add',
                    edit_url: 'usersauth/edit',
                    del_url: 'usersauth/del',
                    multi_url: 'usersauth/multi',
                    table: 'users',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'user_id',
                sortName: 'user_id',
                queryParams: function (params) {
                    var options = table.bootstrapTable('getOptions');
                    var filter = JSON.parse(params.filter);
                    filter.user_status = 0;
                    params.filter = JSON.stringify(filter);
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'phone', title: __('注册手机号'), operate: '='},
                        {field: 'nick_name', title: __('用户昵称'), operate: false},
                        {field: 'real_name', title: __('实名'), operate: '='},
                        {field: 'identity_number', title: __('身份证号'), operate: '='},
                        {field: 'identity_img_list', title: __('身份证照片'), operate: false, formatter: Table.api.formatter.images},
                        {field: 'apply_time', title: __('申请时间'), operate: 'RANGE', addclass: 'datetimerange'},
                        {field: 'refusal_time', title: __('拒绝时间'), operate: false},
                        {field: 'updatetime', title: __('通过时间'), operate: false},
                        {field: 'refusal_reason', title: __('拒绝原因'), operate: false},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.custom
                        },
                    ]
                ]
            });

            $('#table').bootstrapTable('hideColumn', 'refusal_reason');
            $('#table').bootstrapTable('hideColumn', 'refusal_time');

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {

                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    if (typeStr == 'one') {
                        $('#tongguo').show();
                        $('#jujue').show();
                        filter.user_status = 0;
                        $('#table').bootstrapTable('hideColumn', 'refusal_reason');
                        $('#table').bootstrapTable('hideColumn', 'refusal_time');
                        $('#table').bootstrapTable('hideColumn', 'updatetime');
                        $('#table').bootstrapTable('showColumn', 'custom');
                    }

                    if (typeStr == 'two') {
                        $('#tongguo').hide();
                        $('#jujue').hide();
                        filter.user_status = 1;
                        $('#table').bootstrapTable('hideColumn', 'refusal_reason');
                        $('#table').bootstrapTable('hideColumn', 'refusal_time');
                        $('#table').bootstrapTable('showColumn', 'updatetime');
                        $('#table').bootstrapTable('hideColumn', 'custom');
                    }

                    if (typeStr == 'three') {
                        $('#tongguo').hide();
                        $('#jujue').hide();
                        filter.user_status = 10;
                        $('#table').bootstrapTable('showColumn', 'refusal_reason');
                        $('#table').bootstrapTable('showColumn', 'refusal_time');
                        $('#table').bootstrapTable('hideColumn', 'updatetime');
                        $('#table').bootstrapTable('hideColumn', 'custom');
                    }


                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            $(table).on("click", "[data-id].btn-jujue", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                var url = $(that).data("url");
                Layer.prompt({title: __('拒绝原因'), formType: 0}, function (value, index) {
                    Backend.api.ajax({
                        url: url + "?ids=" + id + "&refusal_reason=" + value,
                        data: $(that).closest("form").serialize()
                    }, function () {
                        table.bootstrapTable('refresh', {});
                    });

                    Layer.close(index);
                });
            });

            // 批量预占
            $(document).on("click", "#tongguo", function () {
                var ids = Table.api.selectedids(table);
                Layer.confirm(
                        __('确定批量通过这些用户？'),
                        {icon: 3, title: __('确定批量通过这些用户？'), shadeClose: true},
                        function (index) {
                            $.post('usersauth/updatestatus', {ids: ids.join(",")}, function (res) {
                                table.bootstrapTable('refresh', {});
                            });
                            Layer.close(index);
                        }
                );

            });

            // 批量取消预占
            $(document).on("click", "#jujue", function () {
                var ids = Table.api.selectedids(table);
                Layer.prompt({title: __('拒绝原因'), formType: 0}, function (value, index) {
                    Backend.api.ajax({
                        url: "usersauth/refusal?ids=" + ids.join(",") + "&refusal_reason=" + value,
                        data: $(this).closest("form").serialize()
                    }, function () {
                        table.bootstrapTable('refresh', {});
                    });

                    Layer.close(index);
                });

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                custom: function (value, row, index) {

                    var html = '<a class="btn-change text-success" style="cursor:pointer" data-url="usersauth/updatestatus?ids="' + row.user_id + '" data-id="' + row.user_id + '"><b style="color: #72afd2;">通过</b></a>';
                    html += ' | <a class="btn-jujue" style="cursor:pointer" data-url="usersauth/refusal" data-id="' + row.user_id + '"><b style="color: #f90f0f;">拒绝</b></a>';

                    return html;
                }
            },
        }
    };
    return Controller;
});